package com.drodds.game;

import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import java.lang.InterruptedException;
import android.os.Handler;

public class EngineView 
    extends SurfaceView 
	implements SurfaceHolder.Callback
{	
	private static final String TAG = EngineView.class.getSimpleName();
	
	private GameEngine gameEngine;
	private DrawThread thread;
	
	public EngineView(Context ctext, Handler h) {
	   super(ctext);
	   getHolder().addCallback(this);
	   setFocusable(true);
	   
	   gameEngine = new GameEngine();
	   gameEngine.init(ctext, h);
	   
	   thread = new DrawThread(getHolder(), gameEngine);
   }
	   
	@Override 
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		Log.d(TAG, "Surface Changed...");
		((MainActivity)getContext()).onSurfaceChanged(arg2,arg3);
	}
	
	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		Log.d(TAG, "Surface Destroyed...");
		boolean retry = true;
		
		while (retry) {
			try {
				thread.join();
				retry = false;
			}
			catch (InterruptedException e) {	
			}
		}
	}
	
	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		Log.d(TAG, "Surface Created...");
		thread.setRunning(true);
		thread.start();
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return super.onTouchEvent(event);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {	
	}
	
	public void setDockBounds(Rect r) {
		gameEngine.setDockBounds(r);
	}

	public void Spin() {
		gameEngine.SpinReels();
	}
}
